package com.ifinc.common.generate;

import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.JdbcUtils;

import javax.sql.DataSource;
import java.sql.*;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: luotao
 * Date: 11/30/13
 * Time: 3:39 PM
 */
public class MyRowMapperResultSetExtractor implements ResultSetExtractor {


    private DataSource dataSource;

    private String tableName;

    private String catalog;

    private String schame;

    /**
     * Create a new RowMapperResultSetExtractor.
     *
     * @param rowMapper the RowMapper which creates an object for each row
     */
    public MyRowMapperResultSetExtractor(String catalog, String schame, String tableName, DataSource dataSource, RowMapper rowMapper) {

        this.dataSource = dataSource;
        this.tableName = tableName;
        this.catalog = catalog;
        this.schame = schame;
    }


    public Map extractData(ResultSet rs) throws SQLException {
        Map resultMap = new LinkedHashMap();
        Connection con = DataSourceUtils.getConnection(this.dataSource);
        DatabaseMetaData dbMeta = con.getMetaData();
        ResultSet pkRSet = dbMeta.getPrimaryKeys(catalog, schame, this.tableName);
        Map pkMap = new LinkedHashMap();
        while (pkRSet.next()) {
            System.out.println("****** Comment ******");
            System.out.println("TABLE_CAT : " + pkRSet.getObject(1));
            System.out.println("TABLE_SCHEM: " + pkRSet.getObject(2));
            System.out.println("TABLE_NAME : " + pkRSet.getObject(3));
            System.out.println("COLUMN_NAME: " + pkRSet.getObject(4));
            System.out.println("KEY_SEQ : " + pkRSet.getObject(5));
            System.out.println("PK_NAME : " + pkRSet.getObject(6));
            System.out.println("****** ******* ******");
            String pkName = (String) pkRSet.getObject(4);
            pkMap.put(pkName, pkName);
        }
        ResultSetMetaData rsmd = rs.getMetaData();

        int columnCount = rsmd.getColumnCount();
        Map columnData = new LinkedHashMap();
        Map columnAll = new LinkedHashMap();
        for (int i = 1; i <= columnCount; i++) {

            String key = JdbcUtils.lookupColumnName(rsmd, i);
            if (pkMap.get(key) == null) {
                columnData.put(key, key);
            }
            columnAll.put(key, key);
        }
        String firstName = tableName.substring(0,1).toUpperCase();
        String tableNameForClass = firstName+tableName.substring(1,tableName.length())+"Const";
        resultMap.put("tableNameForJava",tableNameForClass);
        resultMap.put("pkMap",pkMap);
        resultMap.put("columnData",columnData);
        resultMap.put("columnAll",columnAll);
        return resultMap;
    }

}
